//
//  YGFoodDetailViewController.m
//  yuanshangyiliao
//
//  Created by admin on 2021/4/30.
//

#import "YGFoodDetailViewController.h"
#import "YGFoodDetailHeadCell.h"
#import "YGFoodDetailTableCell.h"
#import "YGFoodDetailDescCell.h"

#import "YGFoodDetailModel.h"
#import "YGFoodDetailBLL.h"

@interface YGFoodDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YGFoodDetailModel *foodDetailModel;

@end

@implementation YGFoodDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
    
    __weak __typeof(self)weakSelf = self;
    [YGFoodDetailBLL requestFoodDetailWithID:self.foodId Success:^(YGFoodDetailModel * _Nonnull model) {
        weakSelf.foodDetailModel = model;
        NSLog(@"%@",model);
        [weakSelf.tableView reloadData];
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        static UIView *headView = nil;
        if (headView == nil) {
            headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
            UILabel *label = [[UILabel alloc] init];
            label.text = @"营养成分";
            [headView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.offset(0);
                make.left.offset(10);
            }];
        }
       
        return headView;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 30;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *cellId = @"";
    if (indexPath.section == 0) {
        cellId = NSStringFromClass([YGFoodDetailHeadCell class]);
        YGFoodDetailHeadCell * headCell = [self creatCell:cellId tableView:tableView];
        headCell.model = self.foodDetailModel;
        cell = headCell;
    }else if (indexPath.section == 1) {
        cellId = NSStringFromClass([YGFoodDetailTableCell class]);
        YGFoodDetailTableCell * tableCell = [self creatCell:cellId tableView:tableView];
        tableCell.datas = self.foodDetailModel.foodInfoDel;
        cell = tableCell;
    }else if (indexPath.section == 2) {
        cellId = NSStringFromClass([YGFoodDetailDescCell class]);
        YGFoodDetailDescCell * descCell = [self creatCell:cellId tableView:tableView];
        descCell.desc = self.foodDetailModel.foodContent;
        cell = descCell;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (id)creatCell:(NSString *)cellId tableView:(UITableView *)tableView {
   UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        Class cellClass = NSClassFromString(cellId);
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - Getter

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}



@end
